//
//  signup.m
//  hw3
//
//  Created by fcbayern on 2020/12/5.
//  Copyright © 2020 fcbayern. All rights reserved.
//

#import "signup.h"
#import "AFNetworking.h"
#import "mainpage.h"
#import "Masonry.h"

@interface signup ()
@property(strong,nonatomic)UIButton * Signup;
@property(strong,nonatomic)UILabel * username;
@property(strong,nonatomic)UILabel * password;
@property(strong,nonatomic)UITextField * enterusername;
@property(strong,nonatomic)UITextField * enterpassword;
@end

@implementation signup

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _Signup = [[UIButton alloc]init];
    _username = [[UILabel alloc]init];
    _password = [[UILabel alloc]init];
    _enterpassword = [[UITextField alloc]init];
    _enterusername = [[UITextField alloc]init];
    
    [self.view addSubview:_Signup];
    [self.view addSubview:_username];
    [self.view addSubview:_password];
    [self.view addSubview:_enterusername];
    [self.view addSubview:_enterpassword];
    
    _password.text = @"密码";
    _username.text = @"用户名";
    _username.font = [UIFont systemFontOfSize:20];
    _password.font = [UIFont systemFontOfSize:20];
    _enterusername.layer.borderWidth = 1;
    _enterpassword.layer.borderWidth = 1;
    _enterusername.font = [UIFont systemFontOfSize:15];
    _enterpassword.font = [UIFont systemFontOfSize:15];
    _enterusername.layer.cornerRadius = 10;
    _enterpassword.layer.cornerRadius = 10;
    _Signup.layer.borderWidth =1;
    _Signup.layer.cornerRadius = 10;
    [_Signup setTitle:@"登录" forState:UIControlStateNormal];
    _Signup.font = [UIFont systemFontOfSize:20];
    [_Signup setTitleColor:[UIColor colorWithRed: 0/ 255.0 green:191 / 255.0 blue:255 / 255.0 alpha:1] forState:UIControlStateNormal];
    
    
    
    [_username mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(self.view.frame.size.height*0.2);
        make.left.equalTo(self.view.mas_left).with.offset(self.view.frame.size.width*0.08);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
        make.height.equalTo(self.view.mas_width).multipliedBy(0.1);
        
    }];
    [_password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_username.mas_bottom).with.offset(self.view.frame.size.height*0.1);
        make.left.equalTo(self.view.mas_left).with.offset(self.view.frame.size.width*0.08);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
        make.height.equalTo(self.view.mas_width).multipliedBy(0.1);
        
    }];
    [_enterusername mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_username);
        make.left.equalTo(_username.mas_right).with.offset(self.view.frame.size.width*0.08);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.5);
        make.height.equalTo(self.view.mas_width).multipliedBy(0.1);
        
    }];
    [_enterpassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_password);
        make.left.equalTo(_password.mas_right).with.offset(self.view.frame.size.width*0.08);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.5);
        make.height.equalTo(self.view.mas_width).multipliedBy(0.1);
        
    }];
    [_Signup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_enterpassword.mas_bottom).with.offset(self.view.frame.size.width*0.2);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.4);
        make.height.equalTo(self.view.mas_width).multipliedBy(0.2);
        
    }];
    [_Signup addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}

-(void)btnclick:(id)sender{
    NSString * Username = _enterusername.text;
    NSString * Password = _enterpassword.text;
    NSDictionary * parameters = @{
        @"name":Username,
        @"pwd":Password
    };
    NSString * url = @"http://172.18.176.202:3333/hw3/signup";
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:url parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary * returnjson = (NSDictionary *)responseObject;
        NSString * checklogin = returnjson[@"msg"];
        if([checklogin isEqualToString:@"success"]){
            UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert1 animated:YES completion:nil];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
                mainpage * page = [[mainpage alloc]init];
                [self.navigationController pushViewController:page animated:YES];
            });
        }
        
        else{
            UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录失败" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert1 animated:YES completion:nil];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
            });
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
        
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
